package com.upc.feedish;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.upc.feedish.utils.DataBaseController;
import com.upc.feedish.utils.HuaweiCloudTools;

import java.util.LinkedHashMap;

public class InternetService extends Service {

    interface Callback{
        void onComplete(int code);
    }

    DataBaseController dataBaseController=new DataBaseController();
    boolean flg=false;
    public InternetService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        isLogin(intent.getStringExtra("username"),intent.getStringExtra("password"));
        return super.onStartCommand(intent, flags, startId);
    }
    public void isLogin(String username, String password){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int flg;
                flg =dataBaseController.isLogin(username,password);
                Intent intentB = new Intent();
                intentB.setAction("com.upc.learning.RECEIVER");
                intentB.putExtra("flg",flg);
                intentB.putExtra("token",dataBaseController.getToken());
                sendBroadcast(intentB);
            }
        }).start();
    }

    public void huaweiGetProperties(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    LinkedHashMap<String,Object> res=new LinkedHashMap<>();
                    res=HuaweiCloudTools.getProperties();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Intent intentB = new Intent();
                    intentB.setAction("com.upc.learning.HUAWEI");
                    String temperature = res.get("Temperature").toString();
                    String humidity = res.get("Humidity").toString();
                    String luminance = res.get("Luminance").toString();
                    String lightStatus=res.get("LightStatus").toString();
                    String motorStatus=res.get("MotorStatus").toString();
                    String setTem=res.get("Setting_Tem").toString();
                    String setHum=res.get("Setting_Hum").toString();
                    String setLum=res.get("Setting_Lux").toString();
                    String ifLightOn=res.get("IfLightOn").toString();
                    String ifMotorOn=res.get("IfMotorOn").toString();
                    String personWarning=res.get("PersonWarning").toString();
                    intentB.putExtra("temperature",temperature);
                    intentB.putExtra("humidity",humidity);
                    intentB.putExtra("luminance",luminance);
                    intentB.putExtra("lightStatus",lightStatus);
                    intentB.putExtra("motorStatus",motorStatus);
                    intentB.putExtra("setTem",setTem);
                    intentB.putExtra("setHum",setHum);
                    intentB.putExtra("setLum",setLum);
                    intentB.putExtra("ifLightOn",ifLightOn);
                    intentB.putExtra("ifMotorOn",ifMotorOn);
                    intentB.putExtra("personWarning",personWarning);
                    sendBroadcast(intentB);
                    if(flg){
                        break;
                    }
                }
            }
        }).start();
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        flg=true;
    }

    @Override
    public IBinder onBind(Intent intent) {
        flg=false;
        return new MsgBinder();
    }

    public class MsgBinder extends Binder {
        public InternetService getService(){
            flg=false;
            return InternetService.this;
        }
    }


}